package com.zyso.repository;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Lists;
import com.zyso.pojo.DetailResponse;
import com.zyso.pojo.SearchResponse;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ZYSoRepository {
    /**
     * 搜索资源
     *
     * @param url
     * @param wb  关键字
     * @return
     */
    public List<SearchResponse> search(String url, String wb) {
        String api = StringUtils.join(url, "?wd=", wb);
        JSONObject jsonObject = call(api);
        if (!jsonObject.containsKey("rss")) {
            return Collections.emptyList();
        }
        JSONObject rss = jsonObject.get("rss", JSONObject.class);
        String list = rss.get("list", String.class);
        if (StringUtils.isBlank(list)) {
            return Collections.emptyList();
        }
        JSONObject obj = JSONUtil.parseObj(list);
        Integer recordCount = obj.get("recordcount", Integer.class);
        if (recordCount < 1) {
            return Collections.emptyList();
        }
        if (recordCount == 1) {
            return Lists.newArrayList(obj.get("video", SearchResponse.class));
        }
        return JSONUtil.toList(obj.getJSONArray("video"), SearchResponse.class);
    }

    /**
     * 获取资源详情
     *
     * @param url
     * @param id
     */
    public DetailResponse detail(String url, Integer id) {
        String api = StringUtils.join(url, "?ac=videolist&ids=", id);
        JSONObject jsonObject = call(api);
        if (!jsonObject.containsKey("rss")) {
            return null;
        }
        JSONObject rss = jsonObject.get("rss", JSONObject.class);
        String list = rss.get("list", String.class);
        if (StringUtils.isBlank(list)) {
            return null;
        }
        JSONObject obj = JSONUtil.parseObj(list);
        Integer recordCount = obj.get("recordcount", Integer.class);
        if (recordCount < 1) {
            return null;
        }
        return obj.get("video", DetailResponse.class);
    }

    private JSONObject call(String api) {
        log.debug("call zy api {}", api);
        try {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(3, TimeUnit.SECONDS)
                    .writeTimeout(3, TimeUnit.SECONDS)
                    .readTimeout(3, TimeUnit.SECONDS)
                    .build();
            RequestBody body = new FormBody.Builder().build();
            Request request = new Request.Builder()
                    .url(api)
                    .post(body)
                    .build();
            Response response = okHttpClient.newCall(request).execute();
            if (HttpStatus.OK.value() != response.code()) {
                return new JSONObject();
            }
            return JSONUtil.xmlToJson(response.body().string());
        } catch (Exception e) {
            return new JSONObject();
        }
    }

}
